<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\CoursesAndTimesExpoter;
use App\Admin\Extensions\ExportWithTableName;
use App\Course;

use App\CourseTime;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;

use Illuminate\Support\Facades\DB;


class CourseController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('课程管理');
            $content->description('课程列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('课程管理');
            $content->description('课程修改');

            $content->body($this->form()->edit($id));
        });
    }

    public function course_time($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('课程预定');
            $content->description('列表');
            $content->body(Admin::grid(CourseSubscribe::class, function (Grid $grid) use ($id) {
                $grid->model()->where('baby_id', $id);

                $grid->disableActions();
                $grid->disableCreation();
                $grid->disableRowSelector();
                $grid->id('ID')->sortable();
                $grid->column('baby.name', '宝宝名');
                $grid->column('course.name', '课程名');
                $grid->column('course.course_time', '上课时间');
                $grid->created_at('预约时间');
            }));
        });
    }

    public function course_time_create($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('课程预定');
            $content->description('列表');
            $content->body(Admin::grid(CourseSubscribe::class, function (Grid $grid) use ($id) {
                $grid->model()->where('baby_id', $id);

                $grid->disableActions();
                $grid->disableCreation();
                $grid->disableRowSelector();
                $grid->id('ID')->sortable();
                $grid->column('baby.name', '宝宝名');
                $grid->column('course.name', '课程名');
                $grid->column('course.course_time', '上课时间');
                $grid->created_at('预约时间');
            }));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('课程管理');
            $content->description('创建课程');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Course::class, function (Grid $grid) {

            $month_ages = DB::select('SELECT id, title AS name  FROM month_ages');
            $month_ages = array_combine(array_column($month_ages, 'id'), array_column($month_ages, 'name'));

            $grid->filter(function ($filter) use ($month_ages) {
                $filter->disableIdFilter();
                $filter->like('name', '课程名称');
                $filter->equal('month_age_id', '月龄')->select($month_ages);
            });

            $grid->exporter(new ExportWithTableName('courses_and_times',
                'courses-' . date('Y-m-d', time()),
                ['id', 'course_name', 'course_description', 'max_sub', 'month_age', 'course_times_count'],
                [],
                ['ID', '课程名称', '课程描述', '最大预约人数', '月龄', '课时数']
            ));


            $grid->actions((function ($actions) use ($month_ages) {
                $course_id = $actions->getKey();
                $actions->disableDelete();
                $actions->prepend('<a title="查看课时" href="/admin/course/' . $course_id . '/course_times"> <i class="fa fa-search"></i></a>');
                $actions->prepend('<a title="新增课时" href="/admin/course/' . $course_id . '/course_times/create"> <i class="fa fa-plus"></i></a>');
            }));
            $grid->column('name', '课程名称');
            $grid->column('description', '课程内容')->limit(10);
            $grid->column('max_sub', '默认最大人数');
            $grid->column('month_age_id', '月龄')->display(function ($month_age_id) use ($month_ages) {
                return isset($month_ages[$month_age_id]) ? $month_ages[$month_age_id] : '';
            });
            $grid->column('id', '课时数')->display(function ($id) {
                return CourseTime::where('course_id', $id)->count();
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Course::class, function (Form $form) {
            $month_ages = DB::select('SELECT id, title AS name  FROM month_ages');
            $month_ages = array_combine(array_column($month_ages, 'id'), array_column($month_ages, 'name'));

            $form->text('name', '课程标题')->rules('required|min:2|max:9', [
                'min' => '课程名称最少两个字',
                'max' => '课程名称最多九个字'
            ]);
            $form->number('max_sub', '最大人数');
            $form->select('month_age_id', '月龄')->options($month_ages);
            $form->textarea('description', '课程内容')->rules('required', ['required' => '请填写课程内容']);
        });
    }

}
